package com.xigua.xcar.net.getshopdetail;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.content.Context;
import android.util.Log;

import com.bantouyan.json.Json;
import com.bantouyan.json.JsonArray;
import com.bantouyan.json.JsonObject;
import com.xigua.xcar.R;
import com.xigua.xcar.data.CommonListData;
import com.xigua.xcar.data.ShopInfoData;
import com.xigua.xcar.net.AbstractData;
import com.xigua.xcar.net.AbstractDataManager;

public class GetShopDetailDataManager extends AbstractDataManager {

	private static final String tag = "GetShopDetailDataManager";
	private int requestcode = 1085;
	private GetShopDetailData mData = null;
	private int shopId = 0;
	public GetShopDetailDataManager(Context context) {
		super(context);
	}
	
	public void setShopId(int shopId){
		this.shopId = shopId;
	}
	
	@Override
	public List<NameValuePair> getRequestParam() {
		List<NameValuePair> postParamList = new ArrayList<NameValuePair>();
		BasicNameValuePair nameValuePair1  = new BasicNameValuePair("requestcode",""+requestcode);        
        postParamList.add(nameValuePair1);
        nameValuePair1 = new BasicNameValuePair("param1", shopId+"");
        postParamList.add(nameValuePair1);
		return postParamList;
	}

	@Override
	public AbstractData getData() {
		return mData;
	}

	@Override
	public AbstractData afterParseData(String ss) {
		Log.i(tag, "orign:ss:" + ss);
		String array[] = ss.split(";");
		if(null != array){
			if(null != array[0]){
				ss = array[0];
			}
		}
		ss = ss.replace("\n", "");
		if(ss.indexOf("(")!=-1){
			ss = ss.substring(1);
		}
		if(ss.indexOf(")")!= -1){
			ss = ss.substring(0, ss.length()-1);
		}
		Log.i(tag, "ss:" + ss);
		try{
		JsonArray jsonArray = (JsonArray)Json.parseJsonText(ss);
		if(jsonArray.count()>0){
			mData = new GetShopDetailData();
			JsonObject obj = jsonArray.getJsonObject(0);
//			String shopId = obj.getString("stationid");			//店铺唯一标识
//			String typeid = obj.getString("typeid");			//
			String stationname = obj.getString("stationname");	//店名
			String logopath = obj.getString("logopath");		//logo路径
			String markPrice = obj.getString("markPrice");		//价格
			String longitude = obj.getString("longitude");		//经度
			String latitude = obj.getString("latitude");		//纬度
			String serverlistStr = obj.getString("servicelist");	//服务列表的字符串形式
			String saleListStr = obj.getString("discountlist");
//			String favorRate = obj.getString("favorRate");
			String tel = obj.getString("tel");
//			String poster = obj.getString("poster");
			tel = tel.replace("(", "");
			tel = tel.replace(")", "");
			tel = tel.replace("-", "");
			String addrDesc = obj.getString("addr");
			mData.setShopName(stationname);
			mData.setUrl(logopath);
			Double t;
			t = Double.parseDouble(longitude);
			mData.setLongtitude(t.intValue());
			t = Double.parseDouble(latitude);
			mData.setLatitude(t.intValue());
			if(markPrice.equals("0") || markPrice.equals("null")){
				mData.setDesc("");
			}else{
				mData.setDesc(markPrice);
			}
			ArrayList<ShopInfoData> mShopInfoArray = new ArrayList<ShopInfoData>();
			ShopInfoData data;
			if(!tel.equals("")){
				data = new ShopInfoData();
				data.setmContent(tel);
				data.setType(0);
				data.setmResId(R.drawable.icon_phone);
				mShopInfoArray.add(data);
			}
			data = new ShopInfoData();
			t = (Double.parseDouble(latitude));
			data.setLatitude(t.intValue());
			t = (Double.parseDouble(longitude));
			data.setLongitude(t.intValue());
			data.setmContent(addrDesc);
			data.setType(1);
			data.setmResId(R.drawable.icon_address);
			mShopInfoArray.add(data);
			
			mData.setmArray(mShopInfoArray);
			ArrayList<CommonListData> mServiceInfoArray = new ArrayList<CommonListData>();
			if(null != serverlistStr){
				JsonArray serviceArray = (JsonArray)Json.parseJsonText(serverlistStr);
				for(int i=0;i<serviceArray.count();i++){
					JsonObject object = serviceArray.getJsonObject(i);
					CommonListData commonData = new CommonListData();
					commonData.setmPhoneNum("");
					commonData.setmResId(0);
					commonData.setmShopName(object.getString("content"));
					mServiceInfoArray.add(commonData);
				}
			}

			
			mData.setServiceInfoArray(mServiceInfoArray);
			ArrayList<CommonListData> mSaleInfoArray = new ArrayList<CommonListData>();
			if(null != saleListStr){
				JsonArray saleArray = (JsonArray)Json.parseJsonText(saleListStr);
				for(int i=0;i<saleArray.count();i++){
					JsonObject object = saleArray.getJsonObject(i);
					CommonListData commonData = new CommonListData();
					commonData.setmPhoneNum("");
					commonData.setmResId(0);
					commonData.setmShopName(object.getString("content"));
					mSaleInfoArray.add(commonData);
				}
			}
			mData.setSaleInfoArray(mSaleInfoArray);
		}
		}catch(Exception e){
			mData = null;
		}
		return mData;
	}

}
